﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;
using GoodMan.Infrastructure.Entities;
using GoodMan.Infrastructure.Entities.Repository;

namespace GoodMan.Portal.Biz
{
    public class PolicyBiz
    {
        public static Policy SavePolicy(Policy policyModel)
        {
            if (policyModel.Id > 0)
            {
                var policy = new Policy().Find(policyModel.Id.ToString());
                policy.Price = policyModel.Price;

                policy.StartDate = policyModel.StartDate;
                policy.ExpireDate = policyModel.ExpireDate;

                policy = policy.PostModify();
                if (policy != null)
                {
                    return policy;
                }
            }
            else
            {
                var policy = policyModel.PostAdd();
                policy = policy.PostModify();
                if (policy != null)
                {
                    return policy;
                }
            }

            return null;
        }
        public static bool DelPolicy(int productID)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    ctx.Delete<T_Policy>()
                        .WhereSet(c => c.ProductId, WhereOperator.Equal, productID)
                        .End()
                        .Excute();
                }

                return true;
            }
            catch (Exception ex)
            {
                //日志记录
                return false;
            }
            return false;
        }

        public static Policy GetSpecialPolicy(int productId = 0)
        {
            DateTime datetime=DateTime.Now;
            Policy policy = null;
            
                policy = new Policy().Find(c => c.PolicyType == 2 && c.ProductId == productId && c.IsActive == true && datetime > c.StartDate && datetime < c.ExpireDate);
                 
            return policy;
        }
    }
}
